/***********************************************************************
* Copyright  2023 客户公司名称
* @URL: 
* @ProjectName: 项目名称
* @Description: 功能描述
* Modification Log :
------------------------------------------------------------------------
* Developer                   Date                   Description
* ----------------------------------------------------------------------
* name 陈秋雨			2023-11-17      NULL 
***********************************************************************/
public with sharing class EmployeeListController {
    public EmployeeListController() {

    }

    /**
    * 功能描述
    * @Author   陈秋雨
    * @DateTime 2023-11-09,
    * @param 搜索条件，当前页，页面大小
    * @return   Map<String, Object>
    */
    @AuraEnabled
    public static List<Employee__c> getEmployees(String searchKeyWord){
        // Map<String, Object> resMap = new Map<String, Object>();

        try {
            List<Employee__c> empList=null;
            //查询符合条件的数据
            empList = new List<Employee__c>([
                SELECT   Id             
                        ,Name           //员工名称
                        ,Sex__c         //性别
                        ,Sum_Of_Score__c//总成绩
                        ,Phone__c       //手机号
                        ,Status__c      //状态
                        ,RecordTypeId   //记录类型
                        ,Post2__r.Name  //岗位
                        ,Entry_Time__c  //入职时间
                        FROM Employee__c
                        WHERE Name LIKE : '%' + searchKeyWord + '%' AND Status__c = '实习期' 
                        AND Sum_Of_Score__c >200
                        // LIMIT : pageSize OFFSET : ( ( currentPage - 1 ) * pageSize )
            ]);
            //  // 统计符合条件的总记录数
            //  Integer totalCount = [SELECT COUNT() FROM Employee__c WHERE Name LIKE : '%' + searchKeyWord + '%' AND Status__c = '实习期'];
            //  Integer totalPage = (totalCount + pageSize - 1) / pageSize;
            //  // 封装返回的数据
            //  resMap.put('record', empList);         //查询到的员工
            //  resMap.put('totalPage', totalPage);    //总页数
            
            return empList;
            
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
        //返回数据
    } 
    /**
    * 功能描述  提交转正申请
    * @Author   陈秋雨
    * @DateTime 2023-11-12,
    * @param 描述参数1
    * @return  返回值描述
    */
    @AuraEnabled
    public static void doRegularApplication(List<Employee__c> emps){
        if (emps == null || emps.isEmpty()) {
            return;
        }
        try {
            update emps;

            List<String> ids = new List<String>();
            for (Employee__c emp : emps) {
                ids.add(emp.Id);
            }
            List<Approval.ProcessSubmitRequest> requests = new List<Approval.ProcessSubmitRequest>();
            for (String recordId : ids) {
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
                req.setObjectId(recordId); // 设置要提交审批的记录ID
                requests.add(req);
            }
            // 批量提交审批请求
            List<Approval.ProcessResult> results = Approval.process(requests);
            // 处理批量审批结果
          // ...
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }
}